package com.sunq.studio.sys.controller;

import com.sunq.studio.sys.domain.TbSysBookOrder;
import com.sunq.studio.sys.domain.TbUser;
import com.sunq.studio.sys.service.PayService;
import com.sunq.studio.sys.service.UserService;
import com.sunq.studio.sys.vo.UserCode;
import com.sunq.studio.utils.JsonToOtherUtil;
import com.sunq.studio.utils.OtherPojo;
import com.sunq.studio.utils.ResultObjVO;
import com.sunq.studio.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * Created by sunq on 2019/6/30.
 */
@Api(value = "用户信息接口 ")
@RestController
@RequestMapping("/user")
public class UserController {


    @Autowired
    private UserService userService;

    @Autowired
    private PayService payService;

    @ApiOperation(value = "获取用户信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "用户Id", required = true, dataType = "int")})

    @PostMapping("/getUser")
    public ResultObjVO getUser(@RequestBody String data   ){

         OtherPojo ob = JsonToOtherUtil.rtOtherPojo(data, new TbUser() );
        TbUser user= (TbUser)ob.getObj();
        return ResultUtil.success( userService.getUser(user.getId()) );

    }
    @RequestMapping("/updateUser")
    public ResultObjVO updateUser(@RequestBody String data   ){

        OtherPojo ob = JsonToOtherUtil.rtOtherPojo(data, new TbUser() );
        TbUser user= (TbUser)ob.getObj();

        int i=userService.updateUser(user);
        if(i==1){
            return ResultUtil.success( "ok" );
        }else{
            return ResultUtil.error("501", "error" );

        }


    }

    @RequestMapping("/getUserbyLevel")
    public ResultObjVO getUserbyLevel(@RequestBody String data   ){

        OtherPojo ob = JsonToOtherUtil.rtOtherPojo(data, new TbUser() );
        TbUser user= (TbUser)ob.getObj();
        return ResultUtil.success( userService.getUserbyLevel(user.getVipLevel(),ob.getPage()) );

    }

    @RequestMapping("/getTeacherList")
    public ResultObjVO getTeacherList(@RequestBody String data   ){

        OtherPojo ob = JsonToOtherUtil.rtOtherPojo(data, new TbUser() );
        TbUser user= (TbUser)ob.getObj();
        return ResultUtil.success( userService.getTeacherList(ob.getPage()) );

    }


    @RequestMapping("/getUserWxInfoOpenid")
    public ResultObjVO getUserWxInfoOpenid(@RequestBody String data   ){

        OtherPojo ob = JsonToOtherUtil.rtOtherPojo(data, new UserCode() );
        UserCode userCode= (UserCode)ob.getObj();

        String res=userService.dealUserWxInfo( userCode.getCode() );

        if(StringUtils.isBlank(res)){
            return ResultUtil.error("500","获取微信openid失败" );
        }else{
            return ResultUtil.success(res );
        }



    }
    @RequestMapping("/insertUser")
    public ResultObjVO isnertUser( @RequestBody String data   ){
        OtherPojo ob = JsonToOtherUtil.rtOtherPojo(data, new TbUser() );
        TbUser user= (TbUser)ob.getObj();
        if (user==null){
            user=new TbUser();
        }
        user.setDelFlag(0);
        user.setLastDay(new Date());
        user.setFirstDay(new Date());
        user.setMoney(new BigDecimal(0.0));
        user.setSysAdmin(0);
        userService.insertUser(user);
        return ResultUtil.success( user);
    }

    @RequestMapping("/userLogin")
    public ResultObjVO userLogin( @RequestBody String data   ){
        OtherPojo ob = JsonToOtherUtil.rtOtherPojo(data, new TbUser() );
        TbUser user= (TbUser)ob.getObj();
        if(user==null||StringUtils.isBlank(user.getNickname())){
            return ResultUtil.error("500","用户信息为空");
        }
        TbUser res= userService.userLogin(user);

        if(res!=null){
            return ResultUtil.success( res);
        }else{
            return ResultUtil.error("500","系统异常");
        }

    }


    @PostMapping("/getPayOrderList")
    public ResultObjVO getPayOrderList(@RequestBody String data){
        OtherPojo ob = JsonToOtherUtil.rtOtherPojo(data, new TbUser() );
        TbUser user= (TbUser)ob.getObj();

        List<TbSysBookOrder> res=payService.getOrderByUserId(user.getId(),ob.getPage());


        return ResultUtil.success(res);
    }




}
